package com.baidu.mpks.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class MapUtils {
    public static <E, T> Map<E, T> listToMap(Class<E> key, Class<T> clazz, String methodName, List<T> list) {
        if (!CollectionUtils.isEmpty(list)) {
            try {
                Method method = clazz.getDeclaredMethod(methodName);
                Map<E, T> map = new HashMap<>();
                for (T t : list) {
                    map.put((key.cast(method.invoke(t))), t);
                }
                return map;
            } catch (Exception e) {
                log.error("" + e);
            }
        }
        return new HashMap<>();
    }
}
